package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.BookMapper;
import com.itheima.dao.RecordMapper;
import com.itheima.domain.Book;
import com.itheima.domain.Record;
import com.itheima.domain.User;
import com.itheima.entity.PageResult;
import com.itheima.service.BookService;
import com.itheima.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

    public PageResult selectNewBooks(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page=bookMapper.selectNewBooks();
        return new PageResult(page.getTotal(),page.getResult());
    }

    public Book findById(String id) {
        return bookMapper.findById(id);
    }

    public Integer borrowBook(Book book) {
        Book b=this.findById(book.getId()+"");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        book.setBorrowTime(df.format(new Date()));
        book.setStatus("1");
        book.setPrice(b.getPrice());
        book.setUploadTime(b.getUploadTime());
        return bookMapper.editBook(book);
    }

    public PageResult search(Book book, Integer pageNum, Integer pageSize) {
        //设置分页查询的参数，开始分页
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page=bookMapper.searchBooks(book);
        return new PageResult(page.getTotal(),page.getResult());
    }
/*新增图书*/
    public Integer addBook(Book book) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //设置新增图书的上架时间
        book.setUploadTime(df.format(new Date()));
        return bookMapper.addBook(book);
    }
    //编辑图书
    public Integer editBook(Book book) {
       return bookMapper.editBook(book);
    }
//查询当前借阅图书
    public PageResult searchBorrowed(Book book, User user, Integer pageNum, Integer pageSize) {
        //设置分页查询的参数，开始分页
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page;
        //将当前登陆的用户放入查询条件中
        book.setBorrower(user.getName());
        if("ADMIN".equals(user.getRole())){
            page=bookMapper.selectBorrowed(book);
        }else {
            //如果是普通用户，查询当前用户借阅但未归还图书
            page=bookMapper.selectMyBorrowed(book);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }
//归还图书  id 归还的图书id   user 归还的人员，也就是当前图书的借阅者
    public boolean returnBook(String id, User user) {
        Book book=this.findById(id);  //根据id查询出图书的完整信息
        boolean rb=book.getBorrower().equals(user.getName());//再次核验当前登录人员和图书借阅者是不是同一个人
        //如果是同一个人，允许归还
        if(rb){
            book.setStatus("2");
            bookMapper.editBook(book);
        }
        return rb;
    }
//归还确认
    @Autowired
    private RecordService recordService;
    public Integer returnConfirm(String id) {
        Book book=this.findById(id);
        Record record=this.setRecord(book);
        book.setStatus("0");
        book.setBorrower("");
        book.setBorrowTime("");
        book.setReturnTime("");
        Integer count=bookMapper.editBook(book);
        if(count==1){
            return recordService.addRecord(record);
        }
        return 0;
    }
    //根据图书信息设置借阅记录的信息
    private Record setRecord(Book book) {
        Record record=new Record();
        record.setBookname(book.getName());
        record.setBookisbn(book.getIsbn());
        record.setBorrower(book.getBorrower());
        record.setBorrower(book.getBorrower());
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        record.setBorrowTime(dateFormat.format(new Date()));
        return record;
    }
}
